﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Heroius.Indexer
{
    public partial class TxItem : Form
    {
        public TxItem(bool Edit = false, DbItem target = null)
        {
            InitializeComponent();
            editting = Edit;
            Changed = false;

            if (editting)
            {
                //editting exist one...
                Target = target;
                TbID.Text = Target.ID;
                TbCata.Text = Target.Cata;
                TbContent.Text = Target.Content;
                TbLink.Text = Target.Link;
                TbKey.Text = Target.Keywords;
                //TbKey.Text = KeyString(Target.Keywords);
            }
            else
            {
                //creating new...
                Target = new DbItem();
                Target.ID = Guid.NewGuid().ToString();
                TbID.Text = Target.ID;
            }
        }

        bool editting;
        public bool Changed { get; private set; }
        public DbItem Target;

        private void BtnOK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(TbCata.Text))
            {
                MessageBox.Show("分类 不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(TbContent.Text))
            {
                MessageBox.Show("内容 不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(TbKey.Text))
            {
                MessageBox.Show("关键字 不能为空");
                return;
            }

            Target.Cata = TbCata.Text;
            Target.Content = TbContent.Text;
            Target.Link = TbLink.Text;
            Target.Keywords = TbKey.Text;
            //Target.Keywords.Clear();
            //Target.Keywords.AddRange(StringKey(TbKey.Text));

            Changed = true;
            this.Close();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region Format Trans

        /// <summary>
        /// 执行列表到字符串的转换
        /// </summary>
        /// <param name="Keys"></param>
        /// <returns></returns>
        public static string KeyString(List<string> Keys)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in Keys)
            {
                sb.Append(item);
                sb.Append(' ');
            }
            return sb.ToString().Trim();
        }
        /// <summary>
        /// 执行字符串到列表的转换
        /// </summary>
        /// <param name="Keys"></param>
        /// <returns></returns>
        public static List<string> StringKey(string Keys)
        {
            return Keys.Trim().Split(' ').ToList();
        }

        #endregion
    }
}
